﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace WordXmlComparer
{
    class AppSettings
    {

        private static string defaultCompareExe = "C:\\Program Files\\WinMerge\\WinMergeU.exe";
        public static string CompareExePath
        {
            get { return GetValue("CompareExePath", defaultCompareExe); }
            set { SetValue("CompareExePath", value); }
        }

        private static string defaultCompareExeArguments = "/dl \"Original Word XML\" /dr \"Modified Word XML\" \"{0}\" \"{1}\" ";
        public static string CompareExeArguments
        {
            get { return GetValue("CompareExeArguments", defaultCompareExeArguments); }
            set { SetValue("CompareExeArguments", value); }
        }

        #region internal gets and sets

        private static string GetValue(string key, string DefaultValue)
        {
            if (ConfigurationManager.AppSettings[key] == null)
            {
                return DefaultValue;
            }
            else
            {
                return ConfigurationManager.AppSettings[key];
            }
        }
        private static void SetValue(string key, string value)
        {
            // Open App.Config of executable for writing
            System.Configuration.Configuration config =
              ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.AppSettings.Settings[key] == null)
            {
                // Add an Application Setting
                config.AppSettings.Settings.Add(key, value);               
            }
            else
            {
                // update an application setting
                config.AppSettings.Settings[key].Value = value;

            }
            // Save the configuration file.
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
        
        #endregion

    }
}